﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MvcAtletIS.Helpers;
using MvcAtletIS.Models;

namespace MvcAtletIS.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrators,Redactors")]
    public class ArticlesController : Controller
    {
        private atletisEntities db = new atletisEntities();

        private const string ARTICLES_YEAR = "CompetitionsYear";
        private const string ARTICLES_MONTH = "CompetitionsMonth";

        // GET: /Admin/Articles/1
        public ViewResult Index(int CategoryId, int? Year, int? Month)
        {
            if (Year == null)
            {
                if (Session[ARTICLES_YEAR] != null)
                {
                    Year = Convert.ToInt32(Session[ARTICLES_YEAR]);
                }
                else
                {
                    Year = DateTime.Now.Year;
                }
            }

            if (Month == null)
            {
                if (Session[ARTICLES_MONTH] != null)
                {
                    Month = Convert.ToInt32(Session[ARTICLES_MONTH]);
                }
                else
                {
                    Month = DateTime.Now.Year;
                }
            }

            Session[ARTICLES_MONTH] = Month;
            Session[ARTICLES_YEAR] = Year;

            ViewBag.Year = new SelectList(db.Years, "Year", "Year", Year);
            ViewBag.Month = new SelectList(Months.GetMonths(), "ID", "Name", Month);
            
            ViewBag.ParentCategory = db.ArticlesCategories.SingleOrDefault(ac => ac.ID == CategoryId);
            
            var articles = db.Articles
                .Include("ArticlesCategories")
                .Where(a => a.CategoryId == CategoryId & a.Date.Value.Year == Year & a.Date.Value.Month == Month)
                .OrderByDescending(a => a.Date)
                .ToList();
            
            return View(articles);
        }

        // GET: /Admin/Articles/Details/5
        public ViewResult Details(int id)
        {
            Articles articles = db.Articles.Single(a => a.ID == id);
            return View(articles);
        }

        // GET: /Admin/Articles/Create
        public ActionResult Create(int CategoryId)
        {
            Articles model = new Articles();
            ViewBag.HtmlHelp = "Nový riadok: <br />, <b>bold text</b>, <i>italic text</i>";
            ViewBag.ImagePath = FilesHelper.GetFiles(Server.MapPath("~/Content/articles"), string.Empty);
            model.CategoryId = CategoryId;
            return View(model);
        } 

        // POST: /Admin/Articles/Create
        [HttpPost]
        public ActionResult Create(Articles articles)
        {
            if (ModelState.IsValid)
            {
                db.Articles.AddObject(articles);
                db.SaveChanges();
                return RedirectToAction("Index", new { CategoryId = articles.CategoryId});  
            }

            ViewBag.ImagePath = FilesHelper.GetFiles(Server.MapPath("~/Content/articles"), string.Empty);
            return View(articles);
        }
        
        // GET: /Admin/Articles/Edit/5
        public ActionResult Edit(int id)
        {
            Articles articles = db.Articles.Single(a => a.ID == id);
            ViewBag.HtmlHelp = "Nový riadok: <br />, <b>bold text</b>, <i>italic text</i>";
            ViewBag.ImagePath = FilesHelper.GetFiles(Server.MapPath("~/Content/articles"), articles.ImagePath);
            return View(articles);
        }

        // POST: /Admin/Articles/Edit/5
        [HttpPost]
        public ActionResult Edit(Articles articles)
        {
            if (ModelState.IsValid)
            {
                db.Articles.Attach(articles);
                db.ObjectStateManager.ChangeObjectState(articles, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index", new { CategoryId = articles.CategoryId });
            }

            ViewBag.ImagePath = FilesHelper.GetFiles(Server.MapPath("~/Content/articles"), string.Empty);
            return View(articles);
        }

        // GET: /Admin/Articles/Delete/5
        public ActionResult Delete(int id)
        {
            Articles articles = db.Articles.Single(a => a.ID == id);
            return View(articles);
        }

        // POST: /Admin/Articles/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Articles articles = db.Articles.Single(a => a.ID == id);
            int categoryId = Convert.ToInt32(articles.CategoryId);
            
            db.Articles.DeleteObject(articles);
            db.SaveChanges();
            
            return RedirectToAction("Index", new { CategoryId = categoryId });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        




    }
}